package hichecker.university;

import hichecker.controller.Config;
import hichecker.ui.components.icon_factory.IconFactory.IconType;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * Class representing a requirement which is neccessary to be authorized for the exam but does
 * nothing for the course grade
 */
@XmlRootElement
public class PVL extends Requirement {
	private @XmlElement List<PVLSeries> series;

	/**
	 * Creates a new pvl and sets a default name
	 */
	public PVL() {
		super();
		setName(Config.NEW_VALUE_PVL_NAME);
	}

	/**
	 * Creates a new pvl with a referenced id
	 *
	 * @param id
	 */
	public PVL(UUID id) {
		super(id);
	}

	/**
	 * Creates a new pvl and copies all values out of the referenced pvl
	 *
	 * @param pvl
	 */
	public PVL(PVL pvl) {
		super(pvl);

		series = new ArrayList<PVLSeries>();
		for (PVLSeries ser : pvl.getSeries())
			series.add(new PVLSeries(ser));
	}

	/**
	 * @return the list of series related to this ptl
	 */
	@XmlTransient
	public List<PVLSeries> getSeries() {
		return series = (series == null ? new ArrayList<PVLSeries>() : series);
	}

	/**
	 * @param series the series to add
	 */
	public void addSeries(PVLSeries series) {
		getSeries().add(series);
	}

	/**
	 * Sets the list of series to {@code null}
	 */
	public void clearSeries() {
		series = null;
	}

	/**
	 * Indicates whether a specific series is part of this ptl
	 *
	 * @param series
	 * @return
	 */
	public boolean containsSeries(Series series) {
		if (this.series == null) return false;
		for (Series ser : this.series)
			 if (series.equals(ser)) return true;
		return false;
	}

	@Override
	public float getMaxPoints() {
		float sum = 0;
		if (series == null)
			series = new ArrayList<PVLSeries>();
		for (PVLSeries ser : series)
			sum += ser.getSumMax();
		return sum;
	}

	@Override
	public float getSumPoints(Student student) {
		float sum = 0;
		if (series == null)
			series = new ArrayList<PVLSeries>();

		boolean noPoints = true;
		for (PVLSeries ser : series) {
			float curSum = ser.getSumGainedPoints(student);
			noPoints &= curSum < 0;
			sum += curSum < 0 ? 0 : curSum;
		}
		return noPoints ? -1 : sum;
	}

	@Override
	public List<UniversityPart> getSubParts() {
		List<UniversityPart> parts = new ArrayList<UniversityPart>();
		parts.addAll(getSeries());
		return parts;
	}

	@Override
	public UUID addSubPart(int type) {
		PVLSeries series = new PVLSeries();
		getSeries().add(series);
		return series.getID();
	}

	@Override
	public void removeSubPart(UUID id) {
		getSeries().remove(new PVLSeries(id));
	}

	@Override
	public IconType getIcon() {
		return IconType.FOLDER_PVL;
	}

	@Override
	public IconType getAddIcon() {
		return IconType.ADD_PVL;
	}
}
